/**
 * Created by lenovo on 2017/12/29.
 */
define(['ngApp', 'ngHttp', 'jquery', 'ngConstant', 'ngAlert','bootstrap'], function (ngApp, ngHttp, JQ, ngConstant, ngAlert) {
    ngApp.directive('onFinishRenderFilters', function ($timeout) {
        return {
            restrict: 'A',
            link: function (scope, element, attr) {
                if (scope.$last === true) {
                    $timeout(function () {
                        scope.$emit('ngRepeatFinished');
                    });
                }
            }
        }
    });
    ngApp.controller("leftTab6_1", function ($scope, $http, $timeout, $rootScope, $state, $uibModal, commonFn, $cookies,$window) {
        $scope.$on('ngRepeatFinished', function (ngRepeatFinishedEvent) {
            $rootScope.leftTabHeight = $('#leftTab6_1').height();
            $('.tabBox').height($rootScope.leftTabHeight);
            console.log($rootScope.leftTabHeight);
        });

        $http({
            url: ngConstant.interfaceHost + 'manager/store/findStoreInfoByID',
            method: 'GET',
            params:{
                "id":$cookies.get('modelId')
            },
            headers: {'Authorization':$cookies.get("token")}
        }).then(function(res){
            $scope.modelMsg = res.data.data;
            console.log(res);
        });


        $scope.changeBtnClick = function(){
            console.log(this.item);
            this.item.flag = 1;
        };
        $scope.saveBtnClick = function(){
            this.item.flag = 0;
            $http({
                url: ngConstant.interfaceHost + 'manager/locInv/updateAllocatedQty',
                method: 'POST',
                data:{
                    "id":this.item.id,
                    "allocatedQty":this.item.allocatedQty
                },
                headers: {'Authorization':$cookies.get("token")}
            }).then(function(res){
                ngAlert.successMsg(res.data.data);
            });
        };


        $scope.addQty = function(){
            this.item.allocatedQty++;
        };

        $scope.subQty = function(){
            if (this.item.allocatedQty == 0) {
                return false;
            }
            this.item.allocatedQty--;
        };







        //弹层中
        function getCarSelectMsg2() {
            $http({
                url: ngConstant.interfaceHost + 'admin/itemBrand/findAllNormalBrand',
                method: 'GET',
                headers: {'Authorization':$cookies.get("token")}
            }).then(function(res){
                $scope.meterialBrand = res.data.data;
                // $scope.meterialBrandCode =  $scope.meterialBrand[0].code;
                console.log($scope.meterialBrandCode);
            });

            //分组
            // $http({
            //     url: ngConstant.interfaceHost + 'admin/itemGroup/findAllNormalGroup',
            //     method: 'GET',
            //     headers: {'Authorization':$cookies.get("token")}
            // }).then(function(res){
            //     $scope.meterialGrope = res.data.data;
            //     // $scope.meterialGropeCode =  $scope.meterialGrope[0].code;
            // });


            //分类
            // $http({
            //     url: ngConstant.interfaceHost + 'admin/itemCategory/findAllNormalCategoryByItemBrandCode',
            //     method: 'GET',
            //     params:{
            //         "itemBrandCode":$scope.meterialBrandCode
            //         // "itemGroupCode":$scope.meterialGropeCode
            //     },
            //     headers: {'Authorization':$cookies.get("token")}
            // }).then(function(res){
            //     console.log(res);
            //     $scope.meterialClassify = res.data.data;
            //     // $scope.meterialClassifyCode =  $scope.meterialClassify[0].code;
            // });

            function getCategory(){
                $http({
                    url: ngConstant.interfaceHost + 'admin/itemCategory/findAllNormalCategoryByItemBrandCode',
                    method: 'GET',
                    params:{
                        "itemBrandCode":$scope.meterialBrandCode,
                        "itemGroupCode":$scope.meterialGropeCode
                    },
                    headers: {'Authorization':$cookies.get("token")}
                }).then(function(res){
                    console.log(res);
                    $scope.meterialClassify = res.data.data;
                    // $scope.meterialClassifyCode =  $scope.meterialClassify[0].code;
                });
            }

                    //名称
                    // $http({
                    //     url: ngConstant.interfaceHost + 'admin/item/findAllItemSearch',
                    //     method: 'GET',
                    //     params:{
                    //         "itemBrandCode":$scope.meterialBrandCode,
                    //         "itemCategoryCode":$scope.meterialClassifyCode,
                    //         "carStyleId":$scope.carModelCode
                    //     },
                    //     headers: {'Authorization':$cookies.get("token")}
                    // }).then(function(res){
                    //     console.log(res);
                    //     $scope.meterialName = res.data.data.content;
                    //     // $scope.meterialNameCode =  $scope.meterialName[0].code;
                    // });


            // $scope.meterialGroupChange = function(){
            //     getCategory();
            //     console.log($scope.meterialGropeCode);
            // };
        }

        //
        // //时间
        // var list = {};
        // list.arr =
        //     [
        //         2018,2017, 2016, 2015, 2014, 2013, 2012 ,2011, 2010, 2009, 2008, 2007, 2006, 2005, 2004, 2003, 2002, 2001,2000
        //     ];

        // $scope.listYear = list.arr;


        $scope.createNewItemBtn = function(){
            $scope.layerOutShow = true;

            $scope.creatNewItemMsg = {};

            getCarSelectMsg2();
            //
            $scope.meterialBrandChange = function(){
                $http({
                    url: ngConstant.interfaceHost + 'admin/itemCategory/findAllNormalCategoryByItemBrandCode',
                    method: 'GET',
                    params:{
                        "itemBrandCode":$scope.meterialBrandCode
                    },
                    headers: {'Authorization':$cookies.get("token")}
                }).then(function(res){
                    console.log(res);
                    $scope.meterialClassify = res.data.data;
                    $scope.meterialClassifyCode =  $scope.meterialClassify[0].code;

                    // 名称
                    // $http({
                    //     url: ngConstant.interfaceHost + 'admin/item/findAllItemSearch',
                    //     method: 'GET',
                    //     params:{
                    //         "itemBrandCode":$scope.meterialBrandCode,
                    //         "itemCategoryCode":$scope.meterialClassifyCode
                    //     },
                    //     headers: {'Authorization':$cookies.get("token")}
                    // }).then(function(res){
                    //     console.log(res);
                    //     $scope.meterialName = res.data.data.content;
                    //     console.log($scope.meterialNameCode);
                    //     // $scope.meterialNameCode =  $scope.meterialName[0].code;
                    // });

                });

            };

            // $scope.meterialClassifyChange = function(){
            //     $http({
            //         url: ngConstant.interfaceHost + 'admin/item/findAllItemSearch',
            //         method: 'GET',
            //         params:{
            //             "itemBrandCode":$scope.meterialBrandCode,
            //             "itemCategoryCode":$scope.meterialClassifyCode
            //         },
            //         headers: {'Authorization':$cookies.get("token")}
            //     }).then(function(res){
            //         console.log(res);
            //         $scope.meterialName = res.data.data.content;
            //         // $scope.meterialNameCode =  $scope.meterialName[0].code;
            //
            //     });
            //
            // };

            // $scope.creatNewItemMsg.itemId = $scope.meterialNameCode;

            // $scope.meterialChange = function(){
            //     console.log($scope.meterialNameCode);
            //     $scope.creatNewItemMsg.itemId = $scope.meterialNameCode;
            // };


            $scope.createNewItemSub = function(){
                var select1 = document.getElementsByName("select1")[0];
                var select1bel= select1 .options[select1.selectedIndex].text;
                $scope.creatNewItemMsg.itemBrandCode = select1bel;

                var select2 = document.getElementsByName("select2")[0];
                var select2bel= select2 .options[select2.selectedIndex].text;
                $scope.creatNewItemMsg.itemCategoryCode = select2bel;


                // var select3 = document.getElementsByName("select3")[0];
                // var select3bel= select3 .options[select3.selectedIndex].text;
                // $scope.creatNewItemMsg.itemCode = select3bel;

                $scope.creatNewItemMsg.storeCode = $cookies.get('modelId');
                $scope.creatNewItemMsg.storeName = $cookies.get('modelName');

                console.log($scope.creatNewItemMsg);

                // if ( $scope.creatNewItemMsg.itemId == null || $scope.creatNewItemMsg.itemId == undefined ) {
                //     ngAlert.errorMsg('请选择耗材名称');
                //     return false;
                // }

                $http({
                    url: ngConstant.interfaceHost + 'manager/locInv/createLocInv',
                    method: 'POST',
                    data:$scope.creatNewItemMsg,
                    headers: {'Authorization':$cookies.get("token")}
                }).then(function(res){
                    ngAlert.successMsg(res.data.data);
                    $timeout(function(){
                        $scope.layerOutShow = false;
                    },2000);
                    gettableDataData();
                },function(res){
                    ngAlert.errorMsg(res.data.errMsg);
                });
            };


        };


        //查询数据的函数封装
        function gettableDataData(){
            //查询table中所有数据接口
            $http({
                url: ngConstant.interfaceHost + 'admin/locInv/findSearch',
                method: 'GET',
                params:{
                    "page":$scope.page,
                    "size":ngConstant.size,
                    "storeCode":$cookies.get('modelId'),
                    "sort":"createDate,desc"
                },
                headers: {'Authorization':$cookies.get("token")}
            }).then(function(res){
                //页数数据
                $scope.pageMsg  = res.data.data;
                console.log(res.data.data.content);
                //表格数据
                $scope.tableData = res.data.data.content;
                //全选
                $scope.checked = [];
                $scope.checkedMsg = [];
                $scope.selectAll = function () {
                    if($scope.select_all) {
                        $scope.checked = [];
                        $scope.checkedMsg = [];
                        angular.forEach($scope.tableData, function (i) {
                            i.checked = true;
                            $scope.checked.push(i.id);
                            $scope.checkedMsg.push({"id":i.id});
                        })
                    }else {
                        angular.forEach($scope.tableData, function (i) {
                            i.checked = false;
                            $scope.checked = [];
                            $scope.checkedMsg = [];
                        })
                    }
                };
                $scope.selectOne = function ($event) {
                    console.log($event);
                    angular.forEach($scope.tableData, function (i) {
                        var index = $scope.checked.indexOf(i.id);
                        if(i.checked && index === -1) {
                            $scope.checked.push(i.id);
                            $scope.checkedMsg.push({"id":i.id});
                        } else if (!i.checked && index !== -1){
                            $scope.checked.splice(index, 1);
                            $scope.checkedMsg.splice(index, 1);
                        }
                    });

                    if ($scope.tableData.length === $scope.checked.length) {
                        $scope.select_all = true;
                    } else {
                        $scope.select_all = false;
                    }
                };
            });
        }



        //获取table列表
        $scope.page = 0;
        $scope.page1 = $scope.page + 1;
        $http({
            url: ngConstant.interfaceHost + 'admin/locInv/findSearch',
            method: 'GET',
            params:{
                "page":$scope.page,
                "size":ngConstant.size,
                "storeCode":$cookies.get('modelId'),
                "sort":"createDate,desc"
            },
            headers: {'Authorization':$cookies.get("token")}
        }).then(function(res){
            console.log(res);
            //页数数据
            $scope.pageMsg  = res.data.data;
            $scope.tableData = res.data.data.content;
            //全选
            //点击下一页上一页时   $scope.select_all初始化置空   在点击事件中写入
            $scope.checked = [];
            $scope.checkedMsg = [];
            $scope.selectAll  = function () {
                if($scope.select_all) {
                    $scope.checked = [];
                    $scope.checkedMsg = [];
                    angular.forEach($scope.tableData, function (i) {
                        i.checked  = true;
                        $scope.checked.push(i.id);
                        $scope.checkedMsg.push({"id":i.id});
                    })
                }else {
                    angular.forEach($scope.tableData, function (i) {
                        i.checked  = false;
                        $scope.checked = [];
                        $scope.checkedMsg = [];
                    })
                }
            };
            $scope.selectOne = function ($event) {
                angular.forEach($scope.tableData, function (i) {
                    var index = $scope.checked.indexOf(i.id);
                    if(i.checked  && index === -1) {
                        $scope.checked.push(i.id);
                        $scope.checkedMsg.push({"id":i.id});
                    } else if (!i.checked  && index !== -1){
                        $scope.checked.splice(index, 1);
                        $scope.checkedMsg.splice(index, 1);
                    }
                });

                if ($scope.tableData.length === $scope.checked.length) {
                    $scope.select_all  = true;
                } else {
                    $scope.select_all  = false;
                }
            };

            //下一页
            $scope.nextPageBtn  = function(){
                $scope.select_all = false;
                if ($scope.page == $scope.pageMsg .totalPages - 1) {
                    return false;
                } else {
                    $scope.page++;
                    $scope.page1++;
                    gettableDataData();
                }

            };

            //上一页
            $scope.prevPageBtn  = function(){
                $scope.select_all = false;
                if ($scope.page == 0) {
                    return false;
                } else {
                    $scope.page--;
                    $scope.page1--;
                    gettableDataData();
                }
            };

            //转到第几页
            $scope.myKeyup13  = function(e){
                $scope.select_all = false;
                //IE 编码包含在window.event.keyCode中，Firefox或Safari 包含在event.which中
                var keycode = window.event?e.keyCode:e.which;
                if(keycode==13){
                    $scope.page = $scope.page1 - 1;
                    gettableDataData();
                }
            };

            //首页
            $scope.firstPageBtn  = function(){
                $scope.select_all = false;
                if ($scope.page == 0) {
                    return false;
                }
                $scope.page = 0;
                $scope.page1 = 1;
                gettableDataData();
            };

            //尾页
            $scope.lastPageBtn  = function(){
                $scope.select_all = false;
                if ($scope.page == $scope.pageMsg .totalPages - 1){
                    return false;
                }
                $scope.page = $scope.pageMsg .totalPages - 1;
                $scope.page1 = $scope.pageMsg .totalPages;
                gettableDataData();
            };

            //批量删除
            $scope.deleteTableDataBtn = function(){
                if ($scope.checkedMsg.length == 0) {
                    ngAlert.errorMsg('请选择要删除的数据');
                    return false;
                }
                console.log($scope.checkedMsg);
                ngAlert.open('确认删除本条记录？');
                $('.layui-layer-btn').click(function(){
                    $http({
                        url: ngConstant.interfaceHost + 'manager/locInv/deleteLocInv',
                        method: 'POST',
                        data:angular.toJson($scope.checkedMsg) ,
                        headers: {'Authorization':$cookies.get("token")}
                    }).then(function(res){
                        console.log(res);
                        ngAlert.successMsg(res.data.data);
                        gettableDataData();
                    });
                });
            };


        });



        $scope.closeLay = function(){
            $scope.layerOutShow = false;
        }

    })
});